<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>预收款管理</title>
<link href="${basePath }/css/ui.css" rel="stylesheet" type="text/css">
<jsp:include page="/header.jsp"></jsp:include>
<script type="text/javascript">

	function find() {
		$("#find_product_dialog").find("input[type='text']").val("");
		$("#find_product_dialog").dialog("open");
	}
	
	function sure() {
		var productId = $("#vague_product_info").val();
		var unit = $("#vague_product_unit").val();
		var tr = $("<tr></tr>");
		tr.load("${basePath}/advance/loadProduct.htm?id="+productId+"&unit="+unit, function() {
			if($.trim(tr.html())!="") {
				$(".table_list").append(tr);
				resetIndex();
				resetSum();
			}
			else $.jqalert("查询不到对应商品！！", "提示");
		});
	}
	
	function resetIndex() {
		$.each($(".table_list").find("tr:gt(0)"), function(i, n){
			$(n).find("td:eq(0)").text(i+1);
		});
	}
	
	function delone(a) {
		$(a).parent().parent().remove();
		resetIndex();
		resetSum();
	}
	
	function resetSum() {
		var total = 0;
		$.each($(".table_list").find("tr:gt(0)"), function(i, n){
			var num = Number($(n).find("td:eq(5)").find("input").val());
			var price = Number($(n).find("td:eq(6)").find("input").val());
			total = (num.mul(price)).add(total);
		});
		$("#sum_price").text(total);
		var rate = Number($("#sum_rate").val());
		$("#real_price").text(total.mul(rate));
	}
	
	function clearAll() {
		$.each($(".table_list").find("tr:gt(0)"), function(i, n){
			$(n).remove();
		});
		resetSum();
	}
	
	function buyconfirm() {
		var customerId = $("#final_customer_table").find("input[name='id']").val();
		var productnum = $(".table_list").find("tr").size()-1;
		if(customerId==null||customerId=='')$.jqalert("请录入客户信息！！", "提示");
		else if(productnum==0)$.jqalert("请选择商品！！", "提示");
		else {
			var products = new Array();
			$.each($(".table_list").find("tr:gt(0)"), function(i, n){
				var product = {}
				$.each($(n).find("td"), function(j, l){
					var value = "";
					if(j==5||j==6) value = $(l).find("input").val();
					else value = $(l).text();
					eval("product."+$(l).attr("title")+"=\""+value+"\"");
				});
				product.product_id = $(n).find("input[name='product_id']").val();
				products.push(product);
			});
			var customer = {};
			$.each($("#final_customer_table").find("span[class!='info_s fl']"), function(i, n){
				eval("customer."+$(n).attr("name")+"=\""+$(n).text()+"\"");
			}); 
			customer.id = $("#final_customer_table").find("input[name='id']").val();
			
			var price = {};
			price.sum_price = $("#sum_price").text();
			price.sum_rate = $("#sum_rate").val();
			price.real_price = $("#real_price").text();
			var form = $("<form action='${basePath}/advance/final.htm' method='post'></form>");
			var hidden1 = $("<input type='hidden' name='products' value='"+JSON.stringify(products)+"'>");
			var hidden2 = $("<input type='hidden' name='customer' value='"+JSON.stringify(customer)+"'>");
			var hidden3 = $("<input type='hidden' name='price' value='"+JSON.stringify(price)+"'>");
			form.append(hidden1);form.append(hidden2);form.append(hidden3);
			form.submit();
		}
	}
	
	$(function() {
		$("#sum_rate").change(function() {
			var rate = Number($(this).val());
			var sumPrice = Number($("#sum_price").text());
			$("#real_price").text(sumPrice.mul(rate));
		})
	})
	
</script>
</head>
<body>
    <div class="main">
        <div class="cont">
        	<div id="customer_div">
            	<jsp:include page="../customer/index.jsp"></jsp:include>
            </div>
            <h2 class="title">商品信息：</h2>
            <c:if test="${from eq null }">            
            <div class="ui">
               <div class="label_box" id="temp_div">
                    <span class="sel_label fl">查找商品：</span>
                    <input type="text" id="product_info" class="text_input fl">
                    <input type="hidden" id="vague_product_info">
                    <input type="hidden" id="vague_product_unit">
                    <a href="javascript:;" class="" onclick="find();">查找</a>
                    <a href="javascript:;" class="" onclick="sure();">确定</a>
                </div>
                <jsp:include page="../product/query.jsp"></jsp:include>
            </div>
            </c:if>
            <h2 class="title"> 查询结果：</h2>
            <div class="ui">
                <table class="table_list">
                    <tr class="">
                      <th class="">序号</th>
                      <th>品名</th>
                      <th class="">年份</th>
                      <th class="">批次</th>
                      <th class="">类型</th>
                      <th>数量</th>
                      <th class="">价格(元)</th>
                      <th class="">删除</th>
                    </tr>
                </table>
                <div class="fr">合计金额：<span id="sum_price">0</span>元</span>
                	<br>收款比例：
                	<select id="sum_rate">
                		<option value="0.1">10%</option>
                		<option value="0.2">20%</option>
                		<option value="0.3">30%</option>
                		<option value="0.4">40%</option>
                		<option value="0.5">50%</option>
                		<option value="0.6">60%</option>
                		<option value="0.7">70%</option>
                		<option value="0.8">80%</option>
                		<option value="0.9">90%</option>
                		<option value="1" selected="selected">100%</option>
                	</select>
                	<br>
                	实际金额：<span id="real_price">0</span>元</span></div>
            </div>
            <br><br>
            <h2 class="title"></h2>
            <div class="text_c">
	        	<a href="javascript:;" class="yellow_btn btn" onclick="buyconfirm();">确认</a>
	        	&nbsp;&nbsp;
	        	<a href="javascript:;" onclick="clearAll();">全部清空</a>
	        </div>
            
        </div>
    </div>

</body>
</html>